package com.laowang.framework.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtil {

  //private Logger logger = LoggerFactory.getLogger(RedisUtil.class);
  public static final int REDIS_DB0 = 0;
  public static final int REDIS_DB1 = 1;
  public static final int REDIS_DB2 = 2;

  @Resource
  private RedisTemplate<String, Object> redisTemplate;

  @Resource(name = "redisTemplate")
  ValueOperations<String, Object> operations;

  public RedisUtil() {
  }

  public void setDataBase(int num) {
    JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory)this.redisTemplate.getConnectionFactory();
    jedisConnectionFactory.setDatabase(num);
    this.redisTemplate.setConnectionFactory(jedisConnectionFactory);
    log.info("host:{}, port:{}, database:{}", new Object[]{jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort(), jedisConnectionFactory.getDatabase()});
  }

  public void log() {
    JedisConnectionFactory jedisConnectionFactory = (JedisConnectionFactory)this.redisTemplate.getConnectionFactory();
    log.info("host:{}, port:{}, database:{}", new Object[]{jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort(), jedisConnectionFactory.getDatabase()});
  }

  public void remove(String... keys) {
    String[] var5 = keys;
    int var4 = keys.length;

    for(int var3 = 0; var3 < var4; ++var3) {
      String key = var5[var3];
      this.remove(key);
    }

  }

  public void removePattern(String pattern) {
    Set<String> keys = this.redisTemplate.keys(pattern);
    if (keys.size() > 0) {
      this.redisTemplate.delete(keys);
    }

  }

  public void remove(String key) {
    if (this.exists(key)) {
      this.redisTemplate.delete(key);
    }

  }

  public boolean exists(String key) {
    return this.redisTemplate.hasKey(key);
  }

  public Set<String> keys() {
    return this.redisTemplate.keys("*");
  }

  public Object get(String key) {
    Object result = null;
    result = this.operations.get(key);
    return result;
  }

  public boolean set(String key, Object value) {
    boolean result = false;

    try {
      this.operations.set(key, value);
      result = true;
    } catch (Exception var5) {
      var5.printStackTrace();
    }

    return result;
  }

  public boolean set(String key, Object value, Long expireTime) {
    boolean result = false;

    try {
      this.operations.set(key, value);
      this.redisTemplate.expire(key, expireTime, TimeUnit.DAYS);
      result = true;
    } catch (Exception var6) {
      var6.printStackTrace();
    }

    return result;
  }

  public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
    this.redisTemplate = redisTemplate;
  }
}
